//
//  WBMainViewController.swift
//  SinaApp
//
//  Created by shui on 2018/3/22.
//  Copyright © 2018年 shui. All rights reserved.
//

import UIKit

///主控制器
class WBMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        setupComposeButton()
        
    }
    

    

    /**
     portrait : 竖屏
     landscape : 横屏
     
     - 使用代码控制设备的好处是想在横屏的时候可以单独处理;
     - 设置支持的方向之后,当前的控制器及子控制器都会遵循这个方向;
     - 如果播放视频,通常用 model 展现。
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    

    
    
    //MARK: - 监听方法
    //FIXME: - 没有实现
    ///撰写微博
    @objc private func composeStatus() {
        print("撰写微博")
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.cyan
        let nav = UINavigationController(rootViewController: vc)
        
        present(nav, animated: true, completion: nil)
    }
    
    //MARK: 私有控件
    //撰写按钮
    private lazy var composeButton : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")
}

//extension 在 Swift 中用来切分代码块,可以把相近功能的函数,放在一个 extension中
//MARK: 设置界面
extension WBMainViewController{
    
    ///设置撰写按钮
    private func setupComposeButton(){
        tabBar.addSubview(composeButton)
        
        //计算按钮的高度
        let count = CGFloat(childViewControllers.count)

        //将向内缩进的宽度减少，让按钮的宽度变大，盖住容错点。
        let w = tabBar.bounds.width / count - 1;
        //CGRectInset 正数向内缩进,负数向外扩展
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
   
        //撰写按钮监听方法
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    ///设置所有子控制器
    private func setupChildControllers() {
        
        let array = [
            ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
            ["clsName":"WBUIViewController"],
            ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
            ["clsName":"WBProfileViewController","title":"我的","imageName":"profile"]
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
        
    }
    
    
    /// 使用字典创建一个子控制器
    ///
    /// - Parameter dict: 信息字典
    /// - Returns: 子控制器
    private func controller(dict:[String:String])->UIViewController {
        
        //      1.取得字典内容
        guard let  clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName)as? UIViewController.Type else {
                
                return UIViewController()
        }
        
        //      2.创建视图控制器
        let vc = cls.init()
        vc.title  = title
        //       3. 设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
//        4.设置 tabbar 的标题字体
        
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.orange], for:.highlighted)//设置颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12)], for: .normal);  //设置字体大小(默认 12号)
        
        let nav = WBNavigationController(rootViewController:vc)
        
        return nav
    }
}


